﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLiteHelper;

namespace Remider.Entity
{
    public class Appointment
    {
        public int AppointmentId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public string IsDone { get; set; }
        public int ActionType { get; set; }
        public int LoopInt { get; set; }
        public int AppointmentType { get; set; }
        public DateTime ScheduleDate { get; set; }
        public DateTime? LastRunDate { get; set; }
        public DateTime NextRunDate { get; set; }
        public DateTime? LaterRunDate { get; set; }
        public string IsDelete { get; set; }

        public void Insert()
        {

            var sqLite = MySqLiteFactory.GetFromPool();
            string sql = string.Format(
                    "Insert into Appointment ([AppointmentId] ,[AppointTypeId] ,[AppointActionId] ,[Title],[Content] ,[IsDone] ,[ScheduleDate],[LastRunTime] ,[NextRunTime] ,[NextINT],[IsDelete] ) values(NULL,{0},{1},'{2}','{3}','{4}','{5}',{6},'{7}',{8},'N'); select LAST_INSERT_ROWID();",
                    AppointmentType, ActionType, Title, Content, IsDone, ScheduleDate.ToString("s"), LastRunDate.HasValue ? string.Format("'{0}'", LastRunDate.Value.ToString("s")) : "NULL", NextRunDate.ToString("s"), LoopInt);
            object result = sqLite.ExceuteScalar(sql);
            AppointmentId = int.Parse(result.ToString());
        }

        public void SetDone()
        {
            var sqLite = MySqLiteFactory.GetFromPool();
            string sql = string.Format("Update Appointment set IsDone='Y' where AppointmentId={0}", AppointmentId);
            sqLite.ExcuteNonquery(sql);
        }

        public void SetDelete()
        {
            var sqLite = MySqLiteFactory.GetFromPool();
            string sql = string.Format("Update Appointment set IsDelete='Y' where AppointmentId={0}", AppointmentId);
            sqLite.ExcuteNonquery(sql);
        }

        public void UpdateLastRunTime(DateTime dateTime)
        {
            var sqLite = MySqLiteFactory.GetFromPool();
            string sql = string.Format("Update Appointment set LastRunTime='{0}' where AppointmentId={1}", dateTime.ToString("s"), AppointmentId);
            sqLite.ExcuteNonquery(sql);
        }

        public void UpdateNexeRunTime(DateTime dateTime)
        {
            var sqLite = MySqLiteFactory.GetFromPool();
            string sql = string.Format("Update Appointment set NextRunTime='{0}' where AppointmentId={1}", dateTime.ToString("s"), AppointmentId);
            sqLite.ExcuteNonquery(sql);
        }

        public void SetNextRunDate()
        {
            if (LoopInt == 0)
            {
                SetDone();
            }
            else
            {
                while (NextRunDate < DateTime.Now)
                {
                    NextRunDate = NextRunDate.AddMinutes(LoopInt);
                }
                UpdateNexeRunTime(NextRunDate);
            }
        }


    }
}